import axios from 'axios';

const http = axios.create({
    baseURL: 'http://shop.bluej.cn/api'
});

const post = (url, data) => {
    return new Promise((resolve, reject) => {
        http.post(url, data, {
                headers: {
                    'Content-Type': 'application/json',
                    'x-token': sessionStorage.getItem('token')
                }
            })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err);
            });
    });
};

const put = (url, data) => {
    return new Promise((resolve, reject) => {
        http.put(url, data, {
                headers: {
                    'Content-Type': 'application/json',
                    'x-token': sessionStorage.getItem('isLogin')
                }
            })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err);
            });
    });
};

const postFile = (url, data) => {
    return new Promise((resolve, reject) => {
        http.post(url, data, {
                headers: {
                    'Content-Type': 'multipart/form-data',
                    'x-token': sessionStorage.getItem('isLogin')
                }
            })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err);
            });
    });
};

const get = (url, params) => {
    return new Promise((resolve, reject) => {
        http.get(url, {
                params,
            }, {
                headers: {
                    'x-token': sessionStorage.getItem('isLogin') || null
                }
            })
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            });
    });
};

const getNotToken = (url) => {
    return new Promise((resolve, reject) => {
        http.get(url)
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            });
    });
};

const requestToDelete = url => {
    return new Promise((resolve, reject) => {
        http.delete(url, {
                headers: {
                    'x-token': sessionStorage.getItem('isLogin')
                }
            })
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            });
    });
};


const postCart = (url, data) => {
    return new Promise((resolve, reject) => {
        http.post(url, data, {
                headers: {
                    'Content-Type': 'application/json',
                    'x-token': sessionStorage.getItem('token')
                }
            })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err);
            });
    });
};

export { post, postCart, get, postFile, put, getNotToken, requestToDelete };